//
//  CarlenghtView.m
//  Logistics
//
//  Created by enzhoukeji on 2018/8/12.
//  Copyright © 2018年 puyue.com. All rights reserved.
//

#import "CarlenghtView.h"
#import "CarCVCell.h"
#import "CollectionViewHeadView.h"
#import "CollectionFootView.h"
@interface CarlenghtView ()<UICollectionViewDelegate,UICollectionViewDataSource>
@property (nonatomic,strong) UIView *bigView;
@property (nonatomic,strong) UICollectionView * collectionView;
@property (nonatomic,strong)  UILabel *labelOne;//用车类型
@property (nonatomic,strong)  UILabel *labelTwo;//用车类型
@property (nonatomic,strong) UILabel *labelThree;//用车类型
@property (nonatomic,strong) UIButton *ShutDownbtu;
@property (nonatomic,strong) NSArray *typeArray;//用车类型
@property (nonatomic,strong) NSArray *lenghtArray;//用车类型
@property (nonatomic,strong) NSArray *withArray;//用车类型
@property (nonatomic,strong) NSArray *headTitleArrayt;
@end
@implementation CarlenghtView
-(NSArray *)headTitleArrayt{
    if (!_headTitleArrayt) {
        _headTitleArrayt = [NSArray arrayWithObjects:@"用车类型",@"车长（米）",@"类型",nil];
    }
    return _headTitleArrayt;
}
-(NSArray *)typeArray{
    if (!_typeArray) {
        _typeArray = [NSArray arrayWithObjects:@"整车",@"零担",nil];
    }
    return _typeArray;
}
-(NSArray *)lenghtArray{
    if (!_lenghtArray) {
        _lenghtArray = [NSArray arrayWithObjects:@"1.8",@"2.7",@"3.8",@"4.2",@"5",@"6.2",@"6.8",@"7.7",@"8.2",@"8.7",@"9.6",@"11.7",@"12.5",@"13",@"15",@"16",@"17.5",nil];
    }
    return _lenghtArray;
}
-(NSArray *)withArray{
    if (!_withArray) {
        _withArray = [NSArray arrayWithObjects:@"不限",@"平板",@"高栏",@"厢式",@"不限",@"平板",@"高栏",@"厢式",@"不限",@"平板",@"高栏",@"厢式",@"不限",@"平板",@"高栏",@"厢式",nil];
    }
    return _withArray;
}
- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        self.layer.zPosition = INT8_MAX;
        self.backgroundColor = [UIColor colorWithRed:1/255.0 green:1/255.0 blue:1/255.0 alpha:0.6];
    }
    return self;
}
-(void)createUIView {
    self.bigView = [[UIView alloc]initWithFrame:CGRectMake(GTFixWidthFlaot(20), kMainTopHeight + GTFixHeightFlaot(5), kMainScreen_width-GTFixWidthFlaot(40), kMainScreen_height - kMainTopHeight - GTFixHeightFlaot(30))];
    self.bigView.backgroundColor = [UIColor whiteColor];
    self.bigView.layer.cornerRadius = 10.0;
    [self addSubview:self.bigView];
    //    self.labelOne = [[UILabel alloc]initWithFrame:CGRectMake(10, 5, 100, 20)];
    //    self.labelTwo = [[UILabel alloc]initWithFrame:CGRectMake(10, 5, 100, 20)];
    //    self.labelThree = [[UILabel alloc]initWithFrame:CGRectMake(10, 5, 100, 20)];
    //    self.inputTextField = [[UITextField alloc]initWithFrame:CGRectMake(10, 10, 120, 25)];
    //    self.inputTextField.placeholder = @"收到输入车长";
    self.ShutDownbtu = [[UIButton alloc]initWithFrame:CGRectMake(self.bigView.width - 30, 5, 20, 20)];
    UICollectionViewFlowLayout * flowLayout = [[UICollectionViewFlowLayout alloc] init];
    //1.设置表格的滚动方向 (横向 或者  纵向)
    //    flowLayout.itemSize = CGSizeMake(GTFixWidthFlaot(60), GTFixWidthFlaot(30));
    //    //3.表格间横向的最小间距  (纵向滚动式 效果比较明显)
    //    flowLayout.minimumLineSpacing = 10;
    flowLayout.headerReferenceSize =CGSizeMake(kMainScreen_width,GTReHeightFlaot(50));
    
    //4.表格间纵向的最小间距
    //    flowLayout.minimumInteritemSpacing = 20;
    self.collectionView = [[UICollectionView alloc] initWithFrame:self.bigView.bounds collectionViewLayout:flowLayout];
    self.collectionView.layer.cornerRadius = 10.0;
    self.collectionView.backgroundColor = [UIColor whiteColor];
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    [self.bigView addSubview:self.collectionView];
    //注册cell单元格
    [self.collectionView registerClass:[CarCVCell class] forCellWithReuseIdentifier:@"collection"];
    
    // 注册头视图
    [self.collectionView registerClass:[CollectionViewHeadView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header"];
    //注册尾视图
    [self.collectionView registerClass:[CollectionFootView class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"footView"];
    
}
#pragma mark - 协议方法
//1.返回多少分组
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 3;
}
//2.返回多少行
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    if (section == 0) {
        return  self.typeArray.count;
    }else if(section == 1){
        return self.lenghtArray.count;
    }else{
        return self.withArray.count;
    }
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    
    CarCVCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"collection" forIndexPath:indexPath];
    NSArray *curArray;
    if (indexPath.section == 0) {
        curArray =  self.typeArray;
    }else if(indexPath.section == 1){
        curArray =  self.lenghtArray;
    }else{
        curArray =  self.withArray;
    }
    
    [cell.carButton setTitle:curArray[indexPath.row]forState:UIControlStateNormal];
    cell.carButton.titleLabel.font = [UIFont systemFontOfSize:13];
    cell.carButton.userInteractionEnabled = NO;
    cell.indexPath = indexPath;
    cell.tag = 1000 + indexPath.row;
    return cell;
    
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(nonnull NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        return CGSizeMake(GTFixWidthFlaot(60), GTFixWidthFlaot(25));
    }else if (indexPath.section == 1){
        return CGSizeMake(GTFixWidthFlaot(50), GTFixWidthFlaot(20));
    }else{
        return CGSizeMake(GTFixWidthFlaot(55), GTFixWidthFlaot(25));
    }
}
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    //    [self.collectionView selectItemAtIndexPath:indexPath animated:NO scrollPosition:UICollectionViewScrollPositionNone];
    CarCVCell *cell = (CarCVCell *) [collectionView cellForItemAtIndexPath:indexPath];
    [cell.carButton setBackgroundColor:[UIColor colorWithRed:106/255.0 green:181/255.0 blue:231/255.0 alpha:1]];
    [cell.carButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    cell.carButton.layer.borderColor = [UIColor colorWithRed:106/255.0 green:181/255.0 blue:231/255.0 alpha:1].CGColor;
}
-(void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath{
    CarCVCell *cell = (CarCVCell *) [collectionView cellForItemAtIndexPath:indexPath];
    [cell.carButton setBackgroundColor:[UIColor clearColor]];
    [cell.carButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    cell.carButton.layer.borderColor = [UIColor grayColor].CGColor;
}
//定义每个UICollectionView 的间距
-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
    return UIEdgeInsetsMake(10,10,10,10);
}
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
    
#pragma mark ----- 重用的问题
    //    UICollectionReusableView *header = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header" forIndexPath:indexPath];
    //    header.backgroundColor = [UIColor whiteColor];
    //
    //    if (indexPath.section == 0) {
    //        self.labelOne.text = @"用车类型";
    //        self.labelOne.font = [UIFont systemFontOfSize:14.0f];
    //        self.labelOne.textColor =[UIColor blackColor];
    //        [header addSubview:self.labelOne];
    //        [self.ShutDownbtu setImage:[UIImage imageNamed:@"weorenzheng"] forState:UIControlStateNormal];
    //        [self.ShutDownbtu addTarget:self action:@selector(ShutDownbtuClieck:) forControlEvents:UIControlEventTouchUpInside];
    //        [header addSubview:self.ShutDownbtu];
    //    }else if(indexPath.section == 1){
    //        self.labelTwo.text = @"车长（米）";
    //        self.labelTwo.font = [UIFont systemFontOfSize:14.0f];
    //        [header addSubview:self.labelTwo];
    //    }else {
    //        self.labelThree.text = @"车长（米）";
    //        self.labelThree.font = [UIFont systemFontOfSize:14.0f];
    //        [header addSubview:self.labelThree];
    //    }
    //    return header;
    
    UICollectionReusableView *reusableView =nil;
    if (kind == UICollectionElementKindSectionHeader) {
        CollectionViewHeadView * headView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header" forIndexPath:indexPath];
        headView.titleLab.text =self.headTitleArrayt[indexPath.section];
        reusableView = headView;
    }else{
        CollectionFootView * footView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"footView" forIndexPath:indexPath];
        
        footView.textField.placeholder = @"手动输入车长";
        footView.textField.layer.borderColor = [UIColor grayColor].CGColor;
        footView.textField.layer.borderWidth = 0.5;
        footView.textField.font = [UIFont systemFontOfSize:13];
        
        reusableView = footView;
    }
    return reusableView;
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section {
    
    return CGSizeMake(GTFixWidthFlaot(100),GTFixWidthFlaot(30));
    
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section {
    if (section == 1) {
        return CGSizeMake(GTFixWidthFlaot(100),GTFixWidthFlaot(20));
    }else{
        return CGSizeMake(0,0);
    }
}
-(void)show {
    [self createUIView];
    self.frame = CGRectMake(0, 0, kMainScreen_width, kMainScreen_height);
    [[UIApplication sharedApplication].keyWindow addSubview:self];
}
- (void)dismiss{
    [self removeFromSuperview];
}
-(void)ShutDownbtuClieck:(UIButton *)btn {
    [self dismiss];
}
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    CGPoint point = [[touches anyObject] locationInView:self];
    if (!CGRectContainsPoint(self.bigView.frame, point)) {
        __weak typeof(UIView*)blockview = self.bigView;
        __weak typeof(self)blockself = self;
        __block int blockH = self.frame.size.height;
        [UIView animateWithDuration:0.3 animations:^{
            CGRect bjf = blockview.frame;
            bjf.origin.y = blockH;
            blockview.frame = bjf;
            blockself.alpha = 0.1;
        } completion:^(BOOL finished) {
            [blockself removeFromSuperview];
        }];
    }
}
@end
